<?php
namespace Tool\Service;
use Think\Controller;
use Home\Service\AccountService;
// +----------------------------------------------------------------------
// | Date:2018年3月9日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class AdjustAttService extends Controller{
    
    protected  $config = [];
    protected $errmsg = '';
    protected $dev_id = 0;
    public function __construct($config=[]){
        $defalut = [
            'date'=>'',//必填，修复的日期
            'time_start'=>'13:00:00',//修复的时间开始点
            'time_end'=>'18:00:00',//修复的时间结束点
            're_sn' =>[],//符合条件的工号，默认是全部有效工号
            'include_apply'=>true, //是否不考虑申请情况,默认是包含
        ];
        $this->config = array_merge($defalut,$config);
        $this->config['re_time_start'] = $this->config['date'].' '.$this->config['time_start'];
        $this->config['re_time_end'] = $this->config['date'].' '.$this->config['time_end'];
        $this->config['re_total_min'] = $this->get_min_interval($this->config['re_time_end'],$this->config['re_time_start']);
    }
    
    private function get_min_interval($end,$start){
        $ret = round((strtotime($end) - strtotime($start))/60);
        return $ret;
    }
    
    private function get_employee_clock(){
        $attDayModel = D('AttendanceDay');
        $sql = sql_all_vacation_one();
        if ($this->config['re_sn']) {
            $map['sn'] = ['in',$this->config['re_sn']];
        }
        $map[$sql] =0;//无申请数据
        $map['duty_off'] = ['lt',$this->config['re_time_end']];
        $map['date'] = $this->config['date'];
        $userbox = $attDayModel->where($map)->getField("sn,name,stu_leave_early,$sql as total_vacation,duty_on,duty_off",true);//获取当天有下班打卡并且是时间在 18:00前的员工数据
        return $userbox;
    }
    
    /**
     * 从申请数据中，提取符合条件的用户数据
     * 
     * date:2018年3月9日
     * author: EK_熊
     */
    private function get_employee_apply(){

        //检查当天的申请记录
        if ($this->config['re_sn']) {
            $map['sn'] = ['in',$this->config['re_sn']];
        }
        $map['start_time'] = ['between',[$this->config['date'].' 00:00:00',$this->config['date'].' 23:59:59']];
        $map['end_time'] = ['between',[$this->config['date'].' 00:00:00',$this->config['date'].' 23:59:59']];
        
        //获取当天有申请数据的人，处理合适的早退补偿时间
        $apply = M('Apply')->where($map)->getField('sn,name,start_time,end_time',true);
        $user_list = [];
        if ($apply){
            
            foreach ($apply as $k => $v){
    
                if ($v['end_time'] > $this->config['re_time_end']){
                    $a = 0;
                    continue;
                }elseif($v['end_time'] <= $this->config['re_time_start']){
                    $a = 2;
                    $stu_leave_early = $this->config['re_total_min'];
                }else{
                    $a = 1;
                    $stu_leave_early = $this->get_min_interval($this->config['re_time_end'],$v['end_time']);
                    if ($stu_leave_early == 0) continue;
                };
    
                $user_list[$k] = [
                    'name'=>$v['name'],
                    'sn'=>$k,
                    'stu_leave_early'=>$stu_leave_early,
                    's'=>$v['start_time'],
                    'e'=>$v['end_time'],
                ];
            }
        }
        return $user_list;
    }
    
    public function get_adjust_user(){
        //有打卡的
        $user_clock = $this->get_employee_clock();
        //有申请的
        $user_apply = $this->get_employee_apply();
        //旷工
        $user_absent = $this->get_employee_absenteeism();
        
        
    }
    
    
    public function get_employee_absenteeism(){
        $map['date'] = $this->config['date'];
        $map['sn'] = ['in',$this->config['re_sn']];
        $attday = M('AttendanceDay')->where($map)->getField('sn',true);
        $diff = [];
        if ($attday) {
            foreach ($this->config['re_sn'] as $k=>$v){
                if (!in_array($v,$attday)){
                    $diff[$v] = [
                        'sn'=>$v,
                        'stu_leave_early'=>300,
                    ];
                }
            }
        }
        
        return $diff;
    }
    
    
    /**
     * 调整考勤，填充早退
     * 
     * date:2018年3月9日
     * author: EK_熊
     */
    public function adjust_create(){
        //有打卡的
        $user_clock = $this->get_employee_clock();
        //有申请的
        $user_apply = $this->get_employee_apply();
        //旷工
        $user_absent = $this->get_employee_absenteeism();
        $userbox = $user_clock+$user_apply+$user_absent;
        
        
        if (!$userbox) {
            $this->errmsg = '无合法员工';
            return false;
        }
        
        $count = 0;
        if ($userbox) {
            $account = new AccountService();
            $userbox = $account->append_user_field('sn',$userbox,'sn,dep_id,ep_name as name,dep_name,ep_id');
            $attdayModel = M('AttendanceDay');
            foreach ($userbox as $k=>$v){
                $map_att['sn'] = $v['sn'];
                $map_att['date'] = $this->config['date'];
                $isexist = $attdayModel->where($map_att)->find();
                if (empty($isexist)){
                    $base_add = [
                        'sn'=>$v['sn'],
                        'name'=>$v['name'],
                        'date'=>$this->config['date'],
                        'date_str'=>get_week($this->config['date']),
                        'leave_other'=>$v['stu_leave_early'],//计算请假分钟数
                        'dep_id'=>$v['dep_id'],//计算请假分钟数
                        'dep_name'=>$v['dep_name'],//计算请假分钟数
                        'uid'=>$v['ep_id'],//计算请假分钟数
                        'creaetd_time'=>CURRENT_TIME,
                    ];
                    $ret = $attdayModel->add($base_add);
                    $count ++;                    
                }else{
                    $ret = $attdayModel->where($map_att)->setField('leave_other',$v['stu_leave_early']);
                    $count ++;
                    
                }
            }
        }
        
//         if ($userbox) {
//             $attAdjustModel = M('AttendanceAdjust');
//             foreach ($userbox as $k=>$v){
//                 $map_ex['sn'] = $v['sn'];
//                 $map_ex['date'] = $this->config['date'];
//                 $isexist = $attAdjustModel->where($map_ex)->find();
//                 if (empty($isexist)){
//                     $add = [
//                         'sn'=>$v['sn'],
//                         'name'=>$v['name'],
//                         'ad_min'=>$v['stu_leave_early'],
//                         'ad_total_min'=>$this->config['re_total_min'],
//                         'date'=>$this->config['date'],
//                         'create_time'=>CURRENT_TIME,
//                     ];
        
//                     $ret = $attAdjustModel->add($add);
//                     $count ++;
//                 }else{
//                     $ret = $attAdjustModel->where(['id'=>$isexist['id']])->save([
//                         'name'=>$v['name'],
//                         'ad_min'=>$v['stu_leave_early'],
//                         'ad_total_min'=>$this->config['re_total_min'],
//                         'create_time'=>CURRENT_TIME,
//                     ]);
//                     $count ++;
//                 }
//             }
//         }
        return $count;
    }
    
    public function get_error(){
        return $this->errmsg;
    }
}